import * as React from 'react';
import {Redirect, Route, RouteComponentProps} from "react-router";


interface AuthRouteProps {
    path: string,
    redirect: string,
    component: (props: RouteComponentProps) => JSX.Element,
    right: boolean
}
export class AuthRoute extends React.Component<AuthRouteProps, any> {
    constructor(props: any) {
        super(props);

    }
    public RenderComponent = (props: RouteComponentProps) => {
        const {component: Component} = this.props
        if (this.props.right) {
            return <Component {...props}/>
        } else {
            return <Redirect to={{
                pathname: this.props.redirect,
                state: { from: props.location.pathname}
            }}/>
        }
    }
    public render() {
        return (
            <Route
                path={this.props.path}
                render={this.RenderComponent}
            />
        );
    }
}